package cn.ucox.web.ms.polling;

import cn.ucox.web.ms.redis.RedisClient;

import java.util.HashMap;
import java.util.Map;

import static cn.ucox.web.ms.Constant.*;

/**
 * 同步锚点Redis处理工具
 *
 * @author chenw
 * @create 2017-05-12 16:51
 * @email javacspring@gmail.com
 */
public class SyncRedisUtil {

    private static RedisClient redisClient;

    static {
        redisClient = RedisClient.instance();
    }

    /**
     * 刷新Redis中同步锚点信息
     *
     * @param anchor 同步锚点信息
     */
    static void updateSyncAnchor(SyncAnchor anchor) {
        if (null == anchor)
            anchor = new SyncAnchor();
        SyncAnchor finalAnchor = anchor;
        redisClient.hmset(SYNC_ANCHOR, new HashMap<String, String>() {{
            put(SYNC_ANCHOR_TRANSACTION_ID, String.valueOf(finalAnchor.getTransactionId()));
            put(SYNC_ANCHOR_SYNC_TASK_START_TIME, String.valueOf(finalAnchor.getSyncStartTime()));
            put(SYNC_ANCHOR_SYNC_TASK_END_TIME, String.valueOf(finalAnchor.getSyncEndTime()));
            put(SYNC_ANCHOR_SYNC_DATA_START_TIME, String.valueOf(finalAnchor.getDataStartTime()));
            put(SYNC_ANCHOR_SYNC_DATA_END_TIME, String.valueOf(finalAnchor.getDataEndTime()));
            put(SYNC_ANCHOR_TOTAL_TASK_NUM, String.valueOf(finalAnchor.getTotalTaskNum()));
            put(SYNC_ANCHOR_TRANSACTION_DONE, finalAnchor.getTransProcessDone());
            put(SYNC_ANCHOR_TRANSACTION_DATA_ROWS, String.valueOf(finalAnchor.getTransactionDataRows()));
            put(SYNC_ANCHOR_TRANSACTION_DATA_SIZE, String.valueOf(finalAnchor.getTransactionDataSize()));
            put(SYNC_ANCHOR_TASK_DONE_NUM, String.valueOf(finalAnchor.getTaskDoneNum()));

            put(SYNC_MSST, String.valueOf(finalAnchor.getSyncMediumStartTime()));
            put(SYNC_MSET, String.valueOf(finalAnchor.getSyncMediumEndTime()));
            put(SYNC_MDST, String.valueOf(finalAnchor.getDataMediumStartTime()));
            put(SYNC_MDET, String.valueOf(finalAnchor.getDataMediumEndTime()));

            put(SYNC_LSST, String.valueOf(finalAnchor.getSyncLowStartTime()));
            put(SYNC_LSET, String.valueOf(finalAnchor.getSyncLowEndTime()));
            put(SYNC_LDST, String.valueOf(finalAnchor.getDataLowStartTime()));
            put(SYNC_LDET, String.valueOf(finalAnchor.getDataLowEndTime()));
        }});
    }


    /**
     * 加载上一次同步进度锚点
     *
     * @return SyncAnchor同步锚点信息
     */
    public static SyncAnchor loadSyncAnchorFromRedis() {
        Map<String, String> map = redisClient.hgetAll(SYNC_ANCHOR);
        if (null == map || 0 == map.size()) {
            return new SyncAnchor();
        } else {
            return map2Anchor(map);
        }
    }

    /**
     * MAP转锚点信息类
     *
     * @param map redis map锚点信息
     * @return SyncAnchor
     */
    static SyncAnchor map2Anchor(Map<String, String> map) {
        SyncAnchor anchor = new SyncAnchor();
        anchor.setTransactionId(Long.valueOf(map.getOrDefault(SYNC_ANCHOR_TRANSACTION_ID, "100000000")));//上一次事务ID
        anchor.setSyncStartTime(Long.valueOf(map.getOrDefault(SYNC_ANCHOR_SYNC_TASK_START_TIME, "0")));
        anchor.setSyncEndTime(Long.valueOf(map.getOrDefault(SYNC_ANCHOR_SYNC_TASK_END_TIME, "0")));
        anchor.setDataStartTime(map.getOrDefault(SYNC_ANCHOR_SYNC_DATA_START_TIME, "0"));
        anchor.setDataEndTime(map.getOrDefault(SYNC_ANCHOR_SYNC_DATA_END_TIME,"0"));
        anchor.setTotalTaskNum(Integer.valueOf(map.getOrDefault(SYNC_ANCHOR_TOTAL_TASK_NUM, "0")));
        anchor.setTaskDoneNum(Integer.valueOf(map.getOrDefault(SYNC_ANCHOR_TASK_DONE_NUM, "0")));
        anchor.setTransProcessDone(map.getOrDefault(SYNC_ANCHOR_TRANSACTION_DONE, "N"));
        anchor.setTransactionDataRows(Integer.valueOf(map.getOrDefault(SYNC_ANCHOR_TRANSACTION_DATA_ROWS, "0")));
        anchor.setTransactionDataSize(Long.valueOf(map.getOrDefault(SYNC_ANCHOR_TRANSACTION_DATA_SIZE, "0")));
        anchor.setMaxFtpDetention(Integer.valueOf(map.getOrDefault(KEY_FTP_FILE_DETENTION_NUM, "0")));

        anchor.setSyncMediumStartTime(Long.valueOf(map.getOrDefault(SYNC_MSST, "0")));
        anchor.setSyncMediumEndTime(Long.valueOf(map.getOrDefault(SYNC_MSET, "0")));
        anchor.setDataMediumStartTime(map.getOrDefault(SYNC_MDST, "0"));
        anchor.setDataMediumEndTime(map.getOrDefault(SYNC_MDET,"0"));

        anchor.setSyncLowStartTime(Long.valueOf(map.getOrDefault(SYNC_LSST, "0")));
        anchor.setSyncLowEndTime(Long.valueOf(map.getOrDefault(SYNC_LSET, "0")));
        anchor.setDataLowStartTime(map.getOrDefault(SYNC_LDST, "0"));
        anchor.setDataLowEndTime(map.getOrDefault(SYNC_LDET,"0"));
        return anchor;
    }
}
